package com.hc.demo.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.demo.domain.MyBatisDept;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/23 14:16
 */
@RestController
@RequestMapping("/demo")
public class AxiosController {

    @GetMapping("/fun")
    public Result fun() {
        MyBatisDept dept = new MyBatisDept(10, "sales", "CHICAGO");
        return ResultUtil.success("get()成功了").setData(dept);
    }

    @GetMapping("/fun2/{deptno}")
    public Result fun2(@PathVariable("deptno") Integer deptno) {
        List<MyBatisDept> depts = new ArrayList<>();
        depts.add(new MyBatisDept(10, "ACCOUNTING", "NEWYORK"));
        depts.add(new MyBatisDept(20, "RESEARCH", "DALLAS"));
        depts.add(new MyBatisDept(30, "SALES", "CHICAGO"));
        depts.add(new MyBatisDept(40, "OPERATIONS", "BOSTON"));
        for (MyBatisDept dept : depts) {
            if (dept.getDeptno().equals(deptno)) {
                return ResultUtil.success("找到了").setData(dept);
            }
        }
        return ResultUtil.fail("没有找到");
    }

    @PostMapping("/fun3")
    public Result fun3() {
        return ResultUtil.success("post()成功了");
    }

    @PostMapping("/fun4")
    public Result fun4(Integer deptno, String dname, String loc) {
        System.out.println(deptno + " " + dname + " " + loc);
        return ResultUtil.success("post(Integer deptno, String dname, String loc)成功了");
    }

    @PostMapping("/fun5")
    public Result fun5(MyBatisDept dept) {
        System.out.println(dept);
        return ResultUtil.success("post(Dept dept)成功了");
    }

    @PostMapping(value = "/fun6")
    public Result fun6(@RequestBody MyBatisDept dept) {
        //@RequestBody不能接收表单提交的数据
        System.out.println(dept);
        return ResultUtil.success("post(@RequestBody Dept dept)成功了");
    }

}
